﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace TS_XY_Train_Repaire.UserCon.Locomotive.MainBase.TrainDepSys
{
    public partial class uc_Locomotive_MainBase_TrainDepSys_Source : DevExpress.XtraEditors.XtraForm
    {
        public uc_Locomotive_MainBase_TrainDepSys_Source()
        {
            InitializeComponent();
        }

        private void uc_Locomotive_MainBase_TrainDepSys_Source_Load(object sender, EventArgs e)
        {
            _LoadGrid();
        }

        private void uc_Locomotive_MainBase_TrainDepSys_Source_FormClosing(object sender, FormClosingEventArgs e)
        {

        }
        private void _LoadGrid()
        {
            string strSQL = "select row_number() over (order by f_id) as f_row,* from TS_TR_B_Loco_Fittings_Source where f_del=0 order by f_id asc";
            DataTable dt = new DataTable();
            TS_Win_Class.FilledDataTable(strSQL, dt);
            gc_Main.DataSource = dt;
        }
        private void tb_Name_TextChanged(object sender, EventArgs e)
        {
            bt_Add.Enabled = false;
            bt_Edit.Enabled = false;
            bt_Del.Enabled = false;
            if (tb_Name.Text.Trim().Length > 0)
            {
                bt_Add.Enabled = true;
                if (gv_Main.SelectedRowsCount > 0)
                {
                    bt_Edit.Enabled = true;
                    bt_Del.Enabled = true;
                }
            }
        }

        private void bt_Add_Click(object sender, EventArgs e)
        {
            string strSQL = "insert into TS_TR_B_Loco_Fittings_Source(f_name,f_workerid) values('" + tb_Name.Text.Trim()+"',"+TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+")";
            TS_Win_Class.MySqlCom(strSQL);
            _LoadGrid();
            tb_Name.Text = "";
        }

        private void bt_Edit_Click(object sender, EventArgs e)
        {
            string strSQL = "update TS_TR_B_Loco_Fittings_Source set f_name='" + tb_Name.Text.Trim()+"',f_workerid="+TS_Win_Locomotive_MainForm.pUserInfoMain.GuId+",f_time=getdate() where f_id=" + gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_id").ToString(); 
            TS_Win_Class.MySqlCom(strSQL);
            _LoadGrid();
            tb_Name.Text = "";
        }

        private void bt_Del_Click(object sender, EventArgs e)
        {
            string strSQL = "update TS_TR_B_Loco_Fittings_Source set f_del=1,f_workerid=" + TS_Win_Locomotive_MainForm.pUserInfoMain.GuId + ",f_time=getdate() where f_id=" + gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_id").ToString(); 
            TS_Win_Class.MySqlCom(strSQL);
            _LoadGrid();
            tb_Name.Text = "";
        }

        private void gc_Main_Click(object sender, EventArgs e)
        {
            if (gv_Main.SelectedRowsCount > 0)
            {
                tb_Name.Text = gv_Main.GetRowCellValue(gv_Main.GetSelectedRows()[0], "f_name").ToString();
            }
        }

        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}